package com.ezjie.ielts.module_read.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.model.ExamListDetail;
import com.lidroid.xutils.BitmapUtils;

public class ExamAdapter extends BaseAdapter {

	private Context mContext;
	private BitmapUtils bUtils;
	private List<ExamListDetail> mReadingListData = new ArrayList<ExamListDetail>();

	public ExamAdapter(Context context) {
		mContext = context;
		bUtils = new BitmapUtils(mContext);
		// bUtils.configDefaultLoadingImage(R.drawable.readmain_bg1);//默认背景图片
		// bUtils.configDefaultLoadFailedImage(R.drawable.readmain_bg2);//加载失败图片
		// bUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//设置图片压缩类型
	}

	public void resetData(List<ExamListDetail> mReadingListData) {
		this.mReadingListData = mReadingListData;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mReadingListData.size();
	}

	@Override
	public ExamListDetail getItem(int position) {
		return mReadingListData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_readexam_layout, null);
			vh = new ViewHolder();
			vh.exam_bg = (ImageView) convertView.findViewById(R.id.iv_exam_bg);
			vh.type_bg = (ImageView) convertView.findViewById(R.id.iv_read_type);
			vh.id = (TextView) convertView.findViewById(R.id.tv_exam_id);
			vh.score = (TextView) convertView.findViewById(R.id.tv_exam_score);
			vh.zh_title = (TextView) convertView.findViewById(R.id.tv_exam_zh_title);
			vh.en_title = (TextView) convertView.findViewById(R.id.tv_exam_en_title);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		if (position % 3 == 0 ? true : false) {
			bUtils.configDefaultLoadingImage(R.drawable.readmain_item_bg1);// 默认背景图片
			bUtils.configDefaultLoadFailedImage(R.drawable.readmain_item_bg1);//加载失败图片
		} else if (position % 3 == 1 ? true : false) {
			bUtils.configDefaultLoadingImage(R.drawable.readmain_item_bg2);// 默认背景图片
			bUtils.configDefaultLoadFailedImage(R.drawable.readmain_item_bg2);//加载失败图片
		} else {
			bUtils.configDefaultLoadingImage(R.drawable.readmain_item_bg3);// 默认背景图片
			bUtils.configDefaultLoadFailedImage(R.drawable.readmain_item_bg3);//加载失败图片
		}
		bUtils.display(vh.exam_bg, mReadingListData.get(position).android_img);
		if (mReadingListData.get(position).class_type.equals(ExamListDetail.READS)) {
			vh.type_bg.setBackgroundResource(R.drawable.type_reading);
		} else if (mReadingListData.get(position).class_type.equals(ExamListDetail.LISTEN_CONVERSATIONS)) {
			vh.type_bg.setBackgroundResource(R.drawable.type_conversation);
		} else if (mReadingListData.get(position).class_type.equals(ExamListDetail.LISTEN_LECTURES)) {
			vh.type_bg.setBackgroundResource(R.drawable.type_monologue);
		}
		vh.id.setText("C" + mReadingListData.get(position).book + "-T" + mReadingListData.get(position).task);
		vh.score.setText(mReadingListData.get(position).last_scored);
		vh.zh_title.setText(mReadingListData.get(position).title);
		vh.en_title.setText(Html.fromHtml(mReadingListData.get(position).title_en));
		return convertView;
	}

	class ViewHolder {
		ImageView exam_bg;
		ImageView type_bg;
		TextView id;
		TextView score;
		TextView zh_title;
		TextView en_title;
	}

}
